package nio;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class NIOTest {

    private static void println(Object object) {
        System.out.println(object);
    }

    @Test
    public void testBufferRead() throws IOException {
//        FileInputStream fileInputStream=new FileInputStream("d:/aaa.properties");
//        ByteArrayInputStream byteArrayInputStream;
//        Path path=Paths.get("d:/aaa.properties");
//        BufferedReader bufferedReader=Files.newBufferedReader(path, StandardCharsets.UTF_8);
    }

    @Test
    public void testPathsAndPath() throws IOException {
        //在传统java.io中， 文件和目录都被抽象成File对象， 即 File file = new File(".");
        //NIO.2中则引入接口Path代表与平台无关的路径，文件和目录都用Path对象表示
        //通过路径工具类Paths返回一个路径对象Path
        Path path = Paths.get("d:", "aaa.properties");
        System.out.println("path里包含的路径数量：" + path.getNameCount());
        System.out.println("path的根路径： " + path.getRoot());
        println("文件系统为：" + path.toFile());
        //path的绝对路径
        //对比传统java.io, 取绝对路径 file.getAbsoluteFile()
        Path absolutePath = path.toAbsolutePath();
        System.out.println("path的绝对路径：");
        System.out.println(absolutePath);
        System.out.println("absolutePath的根路径： " + absolutePath.getRoot());
        System.out.println("absolutePath里包含的路径数量：" + absolutePath.getNameCount());
        System.out.println(absolutePath.getName(0));
        //以多个String来构建path
        Path path2 = Paths.get("/", "publish", "codes");
        System.out.println(path2.toString());
        Files.readAllLines(path).forEach(System.out::println);
        try (Stream<String> stringStream = Files.lines(path)) {
            stringStream.forEach(System.out::println);
        }
        Files.copy(path, new FileOutputStream("d:\\xixixi.txt"));
        println(Files.size(path));


    }
}
